//
//  DIYDatePickerView.m
//  UcarTest
//
//  Created by 陈曦 on 16/1/19.
//  Copyright © 2016年 chenxi. All rights reserved.
//

#import "DIYDatePickerView.h"
#import "Define.h"

@interface DIYDatePickerView ()

@property (strong, nonatomic) NSDate *selectDate;

@end

@implementation DIYDatePickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.bgView = [[UIApplication sharedApplication].delegate window].rootViewController.view;
        
        //半透明背景按钮
        self.bgViewBtn = [[UIButton alloc] init];
        [self.bgView addSubview:self.bgViewBtn ];
        [self.bgViewBtn  addTarget:self action:@selector(dismissDatePicker) forControlEvents:UIControlEventTouchUpInside];
        self.bgViewBtn .backgroundColor = [UIColor blackColor];
        self.bgViewBtn .alpha = 0.4;
        self.bgViewBtn .frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);

        self.timePickerBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight / 2 - 64)];
        self.timePickerBgView.backgroundColor = [UIColor whiteColor];
        [self.bgView addSubview:self.timePickerBgView];
        
        //--------------------------时间显示视图--------------------------
        UIView *timeShowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
        timeShowView.backgroundColor = backgroundMainColor;
        [self.timePickerBgView addSubview:timeShowView];
        
        //名字显示标签
        self.nameTimeShowlabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen * 2, 0, kScreenWidthTen * 2, 40)];
        self.nameTimeShowlabel.textColor = labelTextColor;
        self.nameTimeShowlabel.text = @"取车时间:";
        self.nameTimeShowlabel.textAlignment = NSTextAlignmentRight;
        [timeShowView addSubview:self.nameTimeShowlabel];
        
        //时间显示标签
        self.timeShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen * 4, 0, kScreenWidthTen * 4, 40)];
        self.timeShowLabel.text = @"- - - - / - - / - -";
        self.timeShowLabel.textAlignment = NSTextAlignmentCenter;
        self.timeShowLabel.textColor = mainColor;
        [timeShowView addSubview:self.timeShowLabel];
        
        //-----------------------------------------------------------------
        
        //时间选择器
        self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, (kScreenHeight / 2 - 64) / 1.5)];
        self.datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        [self.datePicker addTarget:self action:@selector(datePickerValueChange:) forControlEvents:UIControlEventValueChanged];
        self.datePicker.date = [NSDate date];
        self.datePicker.minimumDate = [NSDate date];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        self.datePicker.maximumDate = [dateFormatter dateFromString:@"2017-01-01 00:00"];
        self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        [self.timePickerBgView addSubview:self.datePicker];
        
        //取消按钮
        self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.cancelBtn.frame = CGRectMake(10, (kScreenHeight / 2 - 64) - 40, kScreenWidthEight, 30);
        [self.cancelBtn setTitleColor:[UIColor colorWithRed:68 / 255.0 green:68 / 255.0 blue:68 / 255.0 alpha:1] forState:UIControlStateNormal];
        [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [self.cancelBtn addTarget:self action:@selector(cancelDeleteView) forControlEvents:UIControlEventTouchUpInside];
        [self.timePickerBgView addSubview:self.cancelBtn];
        
        //确定按钮
        self.confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.confirmBtn.frame = CGRectMake(kScreenWidthEight * 7 - 10, (kScreenHeight / 2 - 64) - 40, kScreenWidthEight, 30);
        [self.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        [self.confirmBtn setTitleColor:[UIColor colorWithRed:0 / 255.0 green:137 / 255.0 blue:225 / 255.0 alpha:1] forState:UIControlStateNormal];
        [self.confirmBtn addTarget:self action:@selector(confirmDeleteView) forControlEvents:UIControlEventTouchUpInside];
        [self.timePickerBgView addSubview:self.confirmBtn];
        
        
    }
    
    return self;
}

#pragma mark DatePicker值改变
- (void)datePickerValueChange:(id)sender
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    self.timeShowLabel.text = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:[sender date]]];
    self.selectDate = [sender date];
    
}

- (void)didFinishSelectedDate:(DateTimeSelect)selectDateTime{
    
    self.blockDate = selectDateTime;
    
}


#pragma mark 点击后删除背景视图
//确定点击删除视图
- (void) confirmDeleteView {
    
    if (self.blockDate) {
        self.blockDate(self.selectDate);
    }
    
    [self cancelDeleteView];
}

//取消点击删除视图
- (void) cancelDeleteView {
    [self.bgViewBtn removeFromSuperview];
    [self.timePickerBgView removeFromSuperview];
}

//蒙板点击删除视图
- (void)dismissDatePicker{
    [self cancelDeleteView];
}

@end
